#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using UTable.Logger;
using System.Xml.Serialization;

namespace UTable.Config
{
    [Serializable]
    public class ClassConfig
    {
        public ClassConfig()
        {
            Version = "1.0.0.0";
            Culture = "neutral";
            PublicKeyToken = "null";
        }

        [XmlAttribute]
        public String Type { get; set; }

        [XmlAttribute]
        public String Assembly { get; set; }

        [XmlAttribute]
        public String Version { get; set; }

        [XmlAttribute]
        public String Culture { get; set; }

        [XmlAttribute]
        public String PublicKeyToken { get; set; }

        [XmlIgnore()]
        public string AssemblyQualifiedName
        {
            get
            {
                return Type + ", " + Assembly + ", Version=" + Version + ", Culture=" + Culture + ", PublicKeyToken=" + PublicKeyToken;
            }
        }

        [XmlIgnore()]
        public Type ClassType
        {
            get
            {
                Type type = null;
                try
                {
                    type = System.Type.GetType(AssemblyQualifiedName);
                }
                catch (System.Exception e)
                {
                    SLogger.Error("Cannot get instance type: " + AssemblyQualifiedName);
                    SLogger.Error(e);
                }
                return type;
            }
        }

        public object CreateInstance()
        {
            return CreateInstance(new object[] { });
        }

        public object CreateInstance(object[] args)
        {
            object instance = null;
            try
            {
                Type type = System.Type.GetType(AssemblyQualifiedName);

                instance = Activator.CreateInstance(type,
                    args);
            }
            catch (System.Exception e)
            {
                SLogger.Error("Cannot load instance: " + AssemblyQualifiedName);
                SLogger.Error(e);
            }
            return instance;
        }
    }
}
